﻿using System.Data;
using SqlSugar;
using StoreAPI.Models;

namespace StoreAPI.Services.ServerImpls
{
    public class UserService : IUserService
    {
        private ISqlSugarClient _db;
        // 把ISqlSugarClient服务注入到UserService中。
        public UserService(ISqlSugarClient db)
        {
            _db = db;
        }
        //登录验证
        public User Login(User model)
        {
            User user = _db.Queryable<User>()
               .Where(x => x.Account == model.Account && x.Password == model.Password)
               .ToList().FirstOrDefault()!;


            return user;
        }
        //获取最大ID
        public int GetMaxId()
        {
            return _db.Queryable<User>()
               .Max(x => x.UserId);
        }
        //判断是否存在
        public bool Exists(int model)
        {
            return _db.Queryable<User>()
               .Any(x => x.UserId == model);
        }
        //注册
        public bool Add(User model)
        {
            return _db.Insertable<User>(model).ExecuteCommand() > 0;
        }
        //更新
        public bool Update(User model)
        {
            return _db.Updateable<User>(model).ExecuteCommand() > 0;
        }
        //删除
        public bool Delete(int primaryKey)
        {
            return _db.Deleteable<User>(primaryKey).ExecuteCommand() > 0;
        }
        //批量删除
        public bool DeleteList(int[] primaryKeys)
        {
            return _db.Deleteable<User>().In(primaryKeys).ExecuteCommand() > 0;
        }
        //查询单个
        public User GetModel(int primaryKey)
        {
            return _db.Queryable<User>()
               .InSingle(primaryKey);
        }
        //查询一行数据转User模型
        public User DataRowToModel(DataRow row)
        {
       
        User user = new User();
           if(row!= null)
            {
                user.UserId = Convert.ToInt32(row["UserId"]);
                user.Account = row["Account"].ToString()!;
                user.Password = row["Password"].ToString()!;
                user.UserTypeId = Convert.ToInt32(row["UserTypeId"]);
                user.Remark = row["Remark"].ToString()!;
                user.Status = Convert.ToInt32(row["Status"]);
                user.CreateUserId = Convert.ToInt32(row["CreateUserId"]);
                user.CreateTime = Convert.ToDateTime(row["CreateTime"]);
                if (!row["LastUpdateUserId"].ToString()!.Equals(DBNull.Value.ToString()))
                {
                    user.LastUpdateUserId = Convert.ToInt32(row["LastUpdateUserId"]);
                }
                if (!row["LastUpdateTime"].ToString()!.Equals(DBNull.Value.ToString()))
                {
                    user.LastUpdateTime = Convert.ToDateTime(row["LastUpdateTime"]);
                }
            }
            return user;
        }
        //查询所有
        public List<User> GetListAll(Expressionable<User> exp)
        {
            return _db.Queryable<User>()
               .Where(exp.ToExpression())
               .ToList();
        }
        //获得前几行数据
        public List<User> GetList(Expressionable<User> exp, int Top)
        {
            return _db.Queryable<User>()
               .Where(exp.ToExpression())
               .Take(Top)
               .ToList();
        }
        //获取记录数
        public int GetRecordCount(Expressionable<User> exp)
        {
            return _db.Queryable<User>()
               .Where(exp.ToExpression())
               .Count();
        }
        //分页查询
        public List<User> GetListByPage(Expressionable<User> exp, out int totalPage, int pageIndex = 1, int pageSize = 10)
        {
            totalPage = 0;
            var result = _db.Queryable<User>()
               .Where(exp.ToExpression())
               .ToPageList(pageIndex, pageSize, ref totalPage);
            return result;
        }
    }
}
